package com.ldc.wandroid.ui.fragments;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.common.model.BasePagingModel;
import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.common.utils.ViewUtils;
import com.ldc.wandroid.adapter.ProjectsArticleAdapter;
import com.ldc.wandroid.databinding.FragmentProjectInfoBinding;
import com.ldc.wandroid.model.ArticleItemModel;
import com.ldc.wandroid.viewmodel.ProjectInfoViewModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProjectInfoFragment extends BaseMvvmFragment<FragmentProjectInfoBinding, ProjectInfoViewModel> {

    private static final String ARG_TITLE = "title";
    private static final String ARG_CID = "cid";

    private ProjectsArticleAdapter articleAdapter;
    private int page;
    private String cid;

    public static ProjectInfoFragment newInstance(String title, String cid) {
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_CID, cid);
        ProjectInfoFragment fragment = new ProjectInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.projectInfo().removeObserver(this::processProjectsArticleModel);
    }

    @Override
    protected FragmentProjectInfoBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentProjectInfoBinding.inflate(inflater, container, false);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        cid = getArguments().getString(ARG_CID, "");
        initAdapter();
        viewModel.projectInfo().observeForever(this::processProjectsArticleModel);
        viewModel.loadProjectInfo(page, cid);

    }

    @Override
    protected ProjectInfoViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, ProjectInfoViewModel.class);
    }


    private void processProjectsArticleModel(BasePagingModel<ArticleItemModel> models) {
        viewBinding.stateView.showContentView();
        viewBinding.rv.refreshView.finishLoadMore();
        viewBinding.rv.refreshView.setNoMoreData(page == models.getPageCount());
        if (page == 0) {
            articleAdapter.setNewData(models.getDatas());
        } else {
            articleAdapter.addNewData(models.getDatas());
        }

    }


    private void initAdapter() {
        viewBinding.rv.refreshView.setEnableRefresh(false);
        articleAdapter = new ProjectsArticleAdapter();
        viewBinding.rv.refreshView.setOnLoadMoreListener(refreshLayout -> {
            page += 1;
            viewModel.loadProjectInfo(page, cid);
        });
        viewBinding.rv.recyclerView.setHasFixedSize(true);
        viewBinding.rv.recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        viewBinding.rv.recyclerView.setAdapter(articleAdapter);
        ViewUtils.recyclerViewScroll(viewBinding.rv.recyclerView, data -> {
            articleAdapter.setScroll(data);
        });
    }
}
